using UnityEngine;
using UnityEditor;

namespace LCH
{
    /// <summary>
    /// 网格绘制器 - 地形工具功能模块
    /// </summary>
    public partial class LchMeshPainter
    {
        /// <summary>
        /// 网格工具实例
        /// </summary>
        MeshTools meshtools = new MeshTools();
        
        /// <summary>
        /// 简单LOD类型索引
        /// </summary>
        public int simpleLodType = 0;
        
        /// <summary>
        /// LOD类型选项
        /// </summary>
        public string[] simpleLodTypes = { "减面算法",/* "减面算法1",*/"平均采样导出" };
        
        /// <summary>
        /// 导出质量选项
        /// </summary>
        public string[] exportTypes = { "低","中", "高"  };
        
        /// <summary>
        /// 导出类型索引
        /// </summary>
        public int simpleExportType = 0;
        
        /// <summary>
        /// Unity网格对象处理
        /// </summary>
        void UnityMeshObj()
        {
            EditorGUI.BeginChangeCheck();
            meshtools.selectingObj = (GameObject)EditorGUILayout.ObjectField("FBX or Obj:", meshtools.selectingObj, typeof(GameObject), false);
            if (null != meshtools.selectingObj)
            {
                meshtools.path = AssetDatabase.GetAssetPath(meshtools.selectingObj);
                meshtools.Check();
            }
            meshtools.smoonthNormalToColor = EditorGUILayout.Toggle("Write the Smoomthed Normal To Color for Cartoon Render", meshtools.smoonthNormalToColor);
            EditorGUI.BeginDisabledGroup(meshtools.selectingObj ==null);
            if (GUILayout.Button("Save"))
            {
                string path = EditorUtility.SaveFolderPanel("Select folder to Export", ".", "");
                if (path.Length > 0)
                {
                    path = path.Substring(Application.dataPath.Length - 6);
                    meshtools.Save(path);
                }
            }
            EditorGUI.EndDisabledGroup();
        }
        
        /// <summary>
        /// 地形转网格功能面板
        /// </summary>
        void TerrainToMesh()
        {
            EditorGUI.BeginChangeCheck();

            //你可以把unity地形转换为unity网格，它拥有更少的面数。
            GUILayout.Label(Languages.GetValue(29, "You can convert unity terrain to unity grid, which has fewer faces."));
            
            terrainTools.terrainData = (TerrainData)EditorGUILayout.ObjectField(Languages.GetValue(30, "Terrain:"), terrainTools.terrainData, typeof(TerrainData), true);
            simpleLodType = EditorGUILayout.Popup("算法类型",simpleLodType, simpleLodTypes);
            if (EditorGUI.EndChangeCheck())
            {
                if (null != terrainTools.terrainData)
                {
                    terrainTools.MakeMesh();
                    //terrainTools.CreateLodMesh(simpleLodType);
                }
            }
            if (null != terrainTools.terrainData)
            {
                if ( null != terrainTools.baseMesh)
                {

                    switch (simpleLodType)
                    {
                        case 0:
                        //case 1:
                            {
                                terrainTools.face = EditorGUILayout.IntSlider(Languages.GetValue(31, "face"), terrainTools.face, 3000, (int)terrainTools.baseMeshCount);


                                if (GUILayout.Button(Languages.GetValue(16, "Save")))
                                {
                                    //string filePath = EditorUtility.SaveFilePanelInProject("保存文件", "MyFile.txt", "txt", "请选择保存文件的位置");
                                    string path = EditorUtility.SaveFilePanelInProject(Languages.GetValue(31, "Select a folder in project to export terrain"), "terrain", "obj", Languages.GetValue(31, "Select a folder in project to export terrain"));
                                    if (terrainTools.face > 5000)
                                    {
                                        terrainTools.BakeMesh();
                                    }
                                    else
                                    {
                                        terrainTools.BakeMesh(100);
                                    }
                                    terrainTools.CreateLodMesh(simpleLodType);
                                    if (path.Length > 0)
                                    {
                                        terrainTools.Save(path);
                                    }
                                }
                            }
                            break;
                        default:
                            {

                                simpleExportType = EditorGUILayout.Popup("导出规格", simpleExportType, exportTypes);
                                if (GUILayout.Button(Languages.GetValue(16, "Save")))
                                {
                                    //string filePath = EditorUtility.SaveFilePanelInProject("保存文件", "MyFile.txt", "txt", "请选择保存文件的位置");
                                    string path = EditorUtility.SaveFilePanelInProject(Languages.GetValue(31, "Select a folder in project to export terrain"), "terrain", "obj", Languages.GetValue(31, "Select a folder in project to export terrain"));

                                    if (path.Length > 0)
                                    {
                                        terrainTools.Save(path, simpleExportType+1);
                                    }
                                }
                            }
                            break;
                    }
                }    
            }
        }
    }
}

